How to authenticate this server for accessing Earth Engine

Link to generate an authentication code for accessing Earth Engine

Step 2 - Click on Accept

Step 3 - Copy the authentication code that is returned

Step 4 - Paste the authentication code below, then run the code by pressing the play button


In [ ]:
authentication_code ='PASTE_YOUR_CODE_HERE'

import ee
import errno
import json
import os
import urllib
import urllib2

from ee.oauthinfo import OAuthInfo

# Try to initialize Earth Engine, and if unsuccessful try to get a credentials file
# using the authentication code provided above.
try:
    ee.Initialize()
except:
    token_request_params = {
      'code': authentication_code,
      'client_id': OAuthInfo.CLIENT_ID,
      'client_secret': OAuthInfo.CLIENT_SECRET,
      'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
      'grant_type': 'authorization_code'
    }
    refresh_token = None
    try:
        response = urllib2.urlopen('https://accounts.google.com/o/oauth2/token',
                               urllib.urlencode(token_request_params)).read()
        tokens = json.loads(response)
        refresh_token = tokens['refresh_token']
    except urllib2.HTTPError, e:
        raise Exception('Problem requesting tokens.  Please try again.  %s %s' %
                    (e, e.read()))

    ### Write refresh token to filesystem for later use
    credentials_path = OAuthInfo.credentials_path()
    dirname = os.path.dirname(credentials_path)
    try:
        os.makedirs(dirname)
    except OSError, e:
        if e.errno != errno.EEXIST:
            raise Exception('Error creating %s: %s' % (dirname, e))

    json.dump({'refresh_token': refresh_token}, open(credentials_path, 'w'))

    print '\nSuccessfully saved authorization to %s' % credentials_path
    
# Try to authenticate to Earth Engine.
try:
  ee.Initialize()
  print '\nSuccessfully authenticated to Earth Engine!'
except:
  print '\nOops. Something went wrong!'

In [ ]: